package net.xdclass.service.impl;

import net.xdclass.controller.Request.AccountLoginRequest;
import net.xdclass.controller.Request.AccountRegisterRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.manager.AccountManager;
import net.xdclass.model.AccountDO;
import net.xdclass.mapper.AccountMapper;
import net.xdclass.model.LoginUser;
import net.xdclass.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.IdUtil;
import net.xdclass.util.JWTUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2025-07-01
 */


@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDO> implements AccountService {
	@Autowired
	private NotifyService notifyService;
	@Autowired
	private AccountManager accountManager;

	@Override
	public JsonData register(AccountRegisterRequest registerRequest) {
		Boolean messagecode =false;

		String to = registerRequest.getPhone();
		String code = registerRequest.getCode();
		if (StringUtils.isNotBlank(to)){
			messagecode = notifyService.checkCode(SendCodeEnum.USER_REGISTER,to,code); }

		if (!messagecode){
			return  JsonData.buildResult(BizCodeEnum.CODE_ERROR);
		}

		AccountDO accountDO = new AccountDO();
		BeanUtils.copyProperties(registerRequest,accountDO);
		accountDO.setAccountNo((Long) IdUtil.generateId());
		accountDO.setSecret("$1$" + CommonUtil.getRandomCode(8));
		String md5Crypt = Md5Crypt.md5Crypt(registerRequest.getPwd().getBytes(),accountDO.getSecret());
		accountDO.setPwd(md5Crypt);
		int row = accountManager.insert(accountDO);
		return JsonData.buildSuccess();
	}

	@Override
	public JsonData login(AccountLoginRequest accountLoginRequest) {
		String phone = accountLoginRequest.getPhone();
		String passwd = accountLoginRequest.getPassword();
		if (StringUtils.isBlank(phone) | StringUtils.isBlank(passwd)){
			return  JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
		}
		List<AccountDO> accountList = accountManager.findByPhone(phone);
		if (accountList !=null && accountList.size() == 1){
			String scrept = accountList.get(0).getSecret();
			String pwd = accountList.get(0).getPwd();
			String md5Crypt = Md5Crypt.md5Crypt(passwd.getBytes(),scrept);
			if(pwd.equals(md5Crypt)) {
				LoginUser loginUser = new LoginUser();
				BeanUtils.copyProperties(accountList.get(0),loginUser);
				return JsonData.buildSuccess(JWTUtil.getToken(loginUser));
			}
			return  JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
		}
		return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
	}
}